import { Component, Input, ViewChild } from '@angular/core';
import { DataTableComponent, TableWidthConfig } from '@devui';
import { JobOrderService } from 'src/app/@core/services/jobOrder/job-order.service';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'app-select-productorder',
  templateUrl: './select-productorder.component.html',
  styleUrls: ['./select-productorder.component.scss']
})
export class SelectProductorderComponent {
  @Input() data: any;
    filterParam = {
      PartNum: '',
      PartName: '',
      JobOrderNum: '',
      JobOrderStatus: '',
      JobOrderProcessStatus: '',
      JobOrderType: '',
      StartTime: '',
      EndTime: '',
      ProcessNum: '',
      ProcessName: '',
      OutsourcingStatus: '',
      PageNumber: 1,
      PageSize: 20,
      IsWithPart: true,
      ZhuanHuanWanDe: false,
    };
    pager = {
      total: 20,
      pageIndex: 1,
      pageSize: 20,
    };
    pageSizeOptions = [10, 20, 30, 50, 100];
    tableData = [];
    filterData = [];
    dataTableOptions = {
      columns: [
        {
          field: '工单号',
          header: '工单号',
          fieldType: 'text',
          fixedLeft: '80px',
        },

        {
          field: '工序序号',
          header: '工序序号',
          fieldType: 'text',
        },
        {
          field: '工序编码',
          header: '工序编码',
          fieldType: 'text',
        },
        {
          field: '工序名称',
          header: '工序名称',
          fieldType: 'text',
        },
        {
          field: '是否检验',
          header: '是否检验',
          fieldType: 'text',
        },
        {
          field: '产品编码',
          header: '产品编码',
          fieldType: 'text',
        },
        {
          field: '产品名称',
          header: '产品名称',
          fieldType: 'text',
        },
        {
          field: '产品规格',
          header: '产品规格',
          fieldType: 'text',
        },
        {
          field: '单位',
          header: '单位',
          fieldType: 'text',
        },
        {
          field: '计划数',
          header: '计划数',
          fieldType: 'text',
        },
        {
          field: '报工数量',
          header: '报工数量',
          fieldType: 'text',
        },
        {
          field: '完成进度',
          header: '完成进度',
          fieldType: 'text',
        },
        {
          field: '剩余完成数',
          header: '剩余完成数',
          fieldType: 'text',
        },
        {
          field: '计划开工时间',
          header: '计划开工时间',
          fieldType: 'text',
        },
        {
          field: '计划完工时间',
          header: '计划完工时间',
          fieldType: 'text',
        },
      ],
    };
    tableWidthConfig: TableWidthConfig[] = [
      {
        field: 'checkbox',
        width: '80px',
      },
      {
        field: '工单号',
        width: '140px',
      },
      {
        field: '工序序号',
        width: '160px',
      },
      {
        field: '工序编码',
        width: '150px',
      },
      {
        field: '工序名称',
        width: '100px',
      },
      {
        field: '是否检验',
        width: '100px',
      },
      {
        field: '产品编码',
        width: '100px',
      },
      {
        field: '产品名称',
        width: '100px',
      },
      {
        field: '产品规格',
        width: '100px',
      },
      {
        field: '单位',
        width: '100px',
      },
      {
        field: '计划委外数',
        width: '100px',
      },
      {
        field: '已委外数',
        width: '100px',
      },
      {
        field: '完成进度',
        width: '180px',
      },
      {
        field: '剩余完成数',
        width: '100px',
      },
      {
        field: '计划开工时间',
        width: '170px',
      },
      {
        field: '计划完工时间',
        width: '170px',
      },
    ];
    msgs: { severity: any; summary: any; content: any }[];
    @ViewChild(DataTableComponent, { static: true })
    datatable: DataTableComponent;
    checkedIds: any = [];
    date = [];
    constructor(
      private SharedMethodService: SharedMethodService,
      private JobOrderService: JobOrderService
    ) {}
    ngOnInit(): void {
      this.filter();
    }
    filter() {
      this.JobOrderService.GetJobOrdersProcess3(this.filterParam).subscribe(
        (data: any) => {

          this.tableData = data.items
            .map((item: any) => {
              return item;
            })
          this.pager.total = data.totalCount;
          this.CheckedRows = this.tableData.filter((item: any) => item.$checked);
        }
      );
    }
    filter2() {
      this.filterParam.PageNumber = 1;
      this.filter();
    }
    reset() {
      this.filterParam = {
        PartNum: '',
        PartName: '',
        JobOrderNum: '',
        JobOrderStatus: '',
        JobOrderProcessStatus: '已派工',
        JobOrderType: '',
        StartTime: '',
        EndTime: '',
        ProcessNum: '',
        ProcessName: '',
        OutsourcingStatus: '',
        PageNumber: 1,
        PageSize: 20,
        IsWithPart: true,
        ZhuanHuanWanDe: false,
      };
      this.filter();
    }
    onRowCheckChange(
      checked: any,
      rowIndex: any,
      nestedIndex: any,
      rowItem: { $checked: any; $halfChecked: boolean }
    ) {
      this.SharedMethodService.onRowCheckChange(
        checked,
        rowIndex,
        nestedIndex,
        rowItem,
        this.datatable
      );
    }
    getCheckedRows() {
      this.SharedMethodService.getCheckedRows(
        this.datatable,
        'jobOrderProcessId'
      ).subscribe((data: any) => {
        this.checkedIds = data;
      })
    } //获取选中行
    CheckedRows = [];
    getCheckedRowsData() {
      this.SharedMethodService.getCheckedRowsData(this.datatable).subscribe(
        (data: any) => {
          this.CheckedRows = data;
          const filterIds = this.data.jobOrderProcessId;
          this.tableData = this.tableData.map((item: any) => {
            if (filterIds.includes(item.jobOrderProcessId)) {
              return {
                ...item,
                $checked: true,
                $halfChecked: false,
              };
            }
            return item;
          });
          this.filterData = this.CheckedRows.filter(
            (row: any) =>
              !this.data.jobOrderProcessId.includes(row.jobOrderProcessId)
          );
        }
      )
    } //获取选中行数据
    cleared() {
      this.reset();
    } //清空选中行
    pageIndexChange($event: any) {
      this.filter();
    } //切换页码
    pageSizeChange($event: any) {
      this.filter();
    } //改变每页显示数量
    showToast(type: any, summary: any, content: any) {
      switch (type) {
        default:
          this.msgs = [{ severity: type, summary: summary, content: content }];
      }
    }//提示框
    getImagesArray(fileURL: string): string[] {
      let urls = fileURL.split(',');
      urls = urls.map((url) => environment.modifiedApiUrl + url);
      return urls;
    }//获取图片数组
    dateChange($event: any) {
      this.filterParam.StartTime = this.date[0].toISOString().split('T')[0];
      this.filterParam.EndTime = this.date[1].toISOString().split('T')[0];
    }
    radio($event: any, rowItem: any) {
      if ($event.target.checked) {
        this.CheckedRows = [rowItem]
      }
    }
}
